<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/12
 * Time: 16:57
 */

namespace app\api\controller\passenger;

use app\api\model\Passenger;
use think\Db;

/**
 * @name【乘客登入接口】
 * @baseurl【passenger】
 * @file【passenger/Login.php】
 */
class Login extends Base
{
    protected $beforeActionList = [];
    /**
     * @name【登录】
     * @desc【登录】
     * @uri【login】
     * @method【post】
     * @param【
     * username $ string $ abc $ 帐号$1#
     * password $ string $ abc $ 密码$1#
     * token $ string $ abc $ 首次登录后返回（直接登录）
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * data $ string $ $ 列表 $ {"passenger_id":9521,"passenger_head_portrait":"new.jjcxfw.com\/upload\/head_portrait\/20180320\/1af134646d3126c700798a8c89495357.jpg","passenger_name":"测试-金文友","passenger_phone":"18626345740","passenger_unit":"晋江市机关单位","car_use_id":256,"is_check":1,"is_instead":1,"user_id":37,"is_set_pwd":null,"role_id":3,"head_url":"http:\/\/new.jjcxfw.com\/upload\/head_portrait\/20180320\/1af134646d3126c700798a8c89495357.jpg","activeTime":1522401136,"loginTime":1522401136,"area_id":4,"token":"9a0f0272f293e127a54e6df40ce2a6b3","ptoken":"a5bfc9e07964f8dddeb95fc584cd965d152240113644"}#
     * @ passenger_id $ int $$ 乘客id $ 1#
     * @ passenger_head_portrait $ string $$ 头像地址 $ 1 #
     * @ passenger_name $ string $$ 乘客姓名 #
     * 】
     */
    public function login()
    {
        $p = input('param.');
        if (!$this->requestMethod) {
            return $this->retError('请求类型错误');
        }
        $where['username'] = $p['username'];
        $where['state']    = 1;  //1-启用
        $user              = Db::name('user')->where($where)->whereNull('delete_time')->find();
        if (!$user) {
            return $this->retError('无此乘客信息/已禁用！');
        }
        if (CorePwd($p['password'], $user['salt']) == $user['password']) {
            $pr_where['a.user_id'] = $user['user_id'];
            $info = (new Passenger())->getPassengerInfo($user, $pr_where);
            $info['activeTime'] = time();
            $info['loginTime']  = time();
            $info['area_id']    = $user['area_id'];
            // $info['token']      = $this->CorePwd($p['password'], $user['salt']);
            $key                = $this->appPrefix . $this->redisPreFix . $user['area_id'] . '.login';
            $field              = md5($user['user_id']);
            $nowTime            = time();
            $timeStamp          = $nowTime . $user['area_id'];
            $info['area_id']    = $user['area_id'];
            $this->redis->hset($key, $field, serialize($info));
            $info['ptoken']     = $field . $timeStamp . $user['area_id'];
            $data['data']       = $info;
            $string             = 'passenger_id|乘客id|乘客表主键id,passenger_head_portrait|头像地址,passenger_name|乘客姓名,passenger_phone|乘客电话';
            $string.= ',passenger_unit|乘客单位,is_check|是否审核员,is_instead|是否代叫车,prop|大于0时为四办VIP,user_id|用户id';
            $string            .= ',is_set_pwd|是否设置密码,role_id|角色id,head_url|头像地址,activeTime|活动时间|用于判断是否超时登入,loginTime|登入时间';
            $string            .= ',area_id|区域id,ptoken|token|请求接口时携带,company_id|单位id';
            $data['explain']    = analyzeDesc($string);
            return $this->retSucc($data);
        } else {
            return $this->retError(104);
        }
    }
}